package com.github.ginirohikocha.dbm.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.ginirohikocha.dbm.Address;
import com.github.ginirohikocha.dbm.R;
import com.github.ginirohikocha.dbm.activity.LoginActivity;
import com.github.ginirohikocha.dbm.activity.MainActivity;
import com.github.ginirohikocha.dbm.activity.OrderDetailActivity;
import com.github.ginirohikocha.dbm.entity.OrderIncludeCustomerWorker;
import com.github.ginirohikocha.dbm.util.HttpUtil;
import com.github.ginirohikocha.dbm.util.JsonUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

public class CompleteCustomerDialog extends Dialog {

    private OrderDetailActivity activity;
    private OrderIncludeCustomerWorker order;

    private TextView name, number, address,
            confirm, cancel;

    public CompleteCustomerDialog(@NonNull Context context, OrderIncludeCustomerWorker order) {
        super(context);
        this.activity = (OrderDetailActivity)context;
        this.order = order;
        init();
    }

    private void init() {
        setContentView(R.layout.dialog_complete_customer);
        name = findViewById(R.id.dialog_complete_customer_name);
        number = findViewById(R.id.dialog_complete_customer_number);
        address = findViewById(R.id.dialog_complete_customer_address);
        confirm = findViewById(R.id.dialog_complete_customer_confirm);
        cancel = findViewById(R.id.dialog_complete_customer_cancel);
        name.setText(order.getCustomername());
        number.setText(order.getTcode());
        address.setText(order.getAddress());
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateCustomer();
                cancel();
            }
        });
    }

    private void updateCustomer() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("customerId", order.getCustomerId());
        jsonObject.put("customerName", name.getText().toString());
        jsonObject.put("customerTcode", number.getText().toString());
        jsonObject.put("customerAddress", address.getText().toString());

        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain; charset=utf-8"), jsonObject.toJSONString());

        HttpUtil.sendOkHttpPostRequest(Address.ORDER_UPDATE_CUSTOMER, requestBody, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity, "服务器连接失败，请检查网络状态", Toast.LENGTH_LONG).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseBody = response.body().string();
                final JSONObject json = JSONObject.parseObject(responseBody);
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity, json.getString("msg"), Toast.LENGTH_LONG).show();
                    }
                });
            }
        });

    }
}
